<?php

/* @var $this yii\web\View */

use yii\helpers\Url;
use yii\helpers\Html;
use common\helpers\Render;
use common\models\Coupon;
use common\models\CouponConfiguration;

$this->addCrumbs('优惠劵列表', 'coupon/coupon-list');
$this->title = isset($coupon['id']) ? '修改' : '添加'. '优惠劵';
?>
<style>
    .condition {position:relative;}
    .flyer-form .close {position:absolute;top:10px;right:10px;font-size:12px;}
</style>

<form class="box-content flyer-form pane gap" id="flyer-create" action="<?= Url::to('@web/coupon/coupon-'.(isset($coupon['id']) ? 'update?id='.$coupon['id'] : 'insert')) ?>" method="post">
    <div class="warn notice mb-10px">
        <p>注：1、本页面中所有金额相关字段，单位均为分，如:1元，对应:100分</p>
        <p>　　2、折扣为百分比，如:打九折，折扣额度为:10</p>
    </div>
    <div class="form-item checker">
        <div class="input-title">标题</div>
        <div class="input-block"><input class="flyer-input" type="text" name="title" value="<?= Render::value($coupon, 'title') ?>" placeholder="title."></div>
    </div>
    <div class="form-item checker">
        <div class="input-title">类型</div>
        <div class="input-block"><?= Render::select('type', Coupon::$typeSelector, Render::value($coupon, 'type'), ['flyer' => 'select']) ?></div>
    </div>
    <div class="form-item item-text">
        <div class="input-title">使用条件<a class="fr cl-blue" href="javascript:add_condition();"><i class="icon-plus"></i>添加</a></div>
        <div class="input-block pl pt pr" id="conditions-value"></div>
    </div>
    <div class="form-item">
        <div class="item-inline checker">
            <div class="input-title">过期方式</div>
            <div class="input-inline"><?= Render::select('timeout', Coupon::$timeoutSelector, Render::value($coupon, 'timeout'), ['flyer' => 'select']) ?></div>
        </div>
        <div class="item-inline checker">
            <div class="input-title">过期周期</div>
            <div class="input-inline"><input class="flyer-input" type="text" name="timeout_value" value="<?= Render::value($coupon, 'timeout_value') ?>" placeholder="timeout value."></div>
        </div>
    </div>
    <div class="form-item">
        <div class="item-inline checker">
            <div class="input-title">优惠方式</div>
            <div class="input-inline"><?= Render::select('mode', Coupon::$modeSelector, Render::value($coupon, 'mode'), ['flyer' => 'select']) ?></div>
        </div>
        <div class="item-inline checker">
            <div class="input-title">优惠额度</div>
            <div class="input-inline"><input class="flyer-input" type="text" name="mode_value" value="<?= Render::value($coupon, 'mode_value') ?>" placeholder="mode value."></div>
        </div>
    </div>
    <div class="form-item checker item-text">
        <div class="input-title">描述</div>
        <div class="input-block"><textarea class="flyer-textarea" name="describe" placeholder="describe."><?= Render::value($coupon, 'describe') ?></textarea></div>
    </div>
    <div class="form-item checker item-text">
        <div class="input-title">备注</div>
        <div class="input-block"><textarea class="flyer-textarea" name="remark" placeholder="remark."><?= Render::value($coupon, 'remark') ?></textarea></div>
    </div>
    <div class="form-item checker">
        <div class="input-title">状态</div>
        <div class="input-block"><?= Render::select('status', Coupon::$statusSelector, Render::value($coupon, 'status'), ['flyer' => 'select']) ?></div>
    </div>
    <div class="form-item">
        <div class="input-block tr bdn"><button class="flyer-button normal border-round" id="save-button" type="submit"><i class="icon-save"></i> 保 存</button></div>
        <textarea id="flyer-create-json" data-form="#flyer-create" style="display:none;"><?= Coupon::checker(isset($coupon['id']) ? 'update' : 'insert') ?></textarea>
    </div>
    <input type="hidden" name="_csrf" value="<?=Yii::$app->request->getCsrfToken() ?>">
</form>

<div style="display:none;">
    <div id="condition">
        <?= Render::select('condition[]', CouponConfiguration::$conditionSelector, null, ['flyer' => 'select']) ?>
    </div>
    <div id="operate">
        <?= Render::select('operate[]', CouponConfiguration::$operateSelector, null, ['flyer' => 'select']) ?>
    </div>
</div>

<script src="<?= Render::static('flyer/flyer.class.js') ?>"></script>
<script src="<?= Render::static('flyer/checker.class.js') ?>"></script>
<script>
    var flyerClass = new flyer();
    var _condition_template =
        '<div class="form-item condition">' +
        '   <div class="item-inline checker">' +
        '      <div class="input-title">判断依据</div>' +
        '      <div class="input-inline">' + $('#condition').html() + '</div>' +
        '   </div>' +
        '   <div class="item-inline checker">' +
        '      <div class="input-title">对比方式</div>' +
        '      <div class="input-inline">' + $('#operate').html() + '</div>' +
        '   </div>' +
        '   <div class="item-inline checker">' +
        '      <div class="input-title">阈值</div>' +
        '      <div class="input-inline"><input class="flyer-input" type="text" name="threshold[]" value="{threshold}" placeholder="threshold."></div>' +
        '   </div>' +
        '   <a class="close cl-red" onclick="move_condition(this);" href="javascript:;"><i class="icon-remove"></i>删除</a>' +
        '</div>';
    // 删除规格值
    function move_condition(mthis)
    {
        if ($(mthis).parent('.condition').siblings().length < 1) {
            return;
        }
        $(mthis).parent('.condition').remove();
    }
    // 添加规格值
    function add_condition(condition = null)
    {
        if(condition === null) {
            condition = { condition: '', operate: '', threshold: '' };
        }
        var _template = _condition_template.replace('{threshold}', condition.threshold);
        $('#conditions-value').append(_template);
        $("#conditions-value [name='operate[]']:last-child").val(condition.operate);
        $("#conditions-value [name='condition[]']:last-child").val(condition.condition);

        flyerClass.renderSelect($("#conditions-value [name='operate[]']:last-child"));
        flyerClass.renderSelect($("#conditions-value [name='condition[]']:last-child"));
    }
    $(document).ready(function() {
        // Form 元素初始化
        flyerClass.init({ form: '#flyer-create' });
        // 表单数据验证
        (new checker).init({ ruleDom: '#flyer-create-json' });
    
        <?php if(isset($coupon) && $coupon->configurations) { ?>
        <?php $conditions = $coupon->getConfigurations()->asArray()->all(); ?>
        <?php foreach($conditions as $condition) { ?>
        add_condition(<?= json_encode($condition) ?>);
        <?php } ?>
        <?php } else { ?>
        add_condition();
        <?php } ?>
    });
</script>